﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using WeiBeeCommon.DataTypes;

namespace WeiBeeCommon.Helpers
{
    public class SearchKeywords
    {
        public string City;
        public List<string> Keywords;
    }
    public class SearchHelper
    {
        private string _keywordsfile = "keywords.xml";
        private SearchKeywords _searchKeywords = null;

        public SearchHelper()
        {
            Load();
        }


        /// <summary>
        /// Change the store file, default name is "keywords.xml"
        /// </summary>
        /// <param name="newfilename">new filename</param>
        public void ChangeStoreFile(string newfilename)
        {
            _keywordsfile = newfilename;
        }

        /// <summary>
        /// Get the default searchkeywords
        /// </summary>
        /// <returns></returns>
        public SearchKeywords DefaultSearchKeywords()
        {
            return new SearchKeywords {City = "", Keywords = new List<string> {"高兴", "快乐", "兴奋"}};
        }

        /// <summary>
        /// Save current search keywords
        /// </summary>
        /// <param name="sk">SearchKeywords</param>
        public void Save(SearchKeywords sk)
        {
            using (var fs = new FileStream(_keywordsfile, FileMode.Create, FileAccess.Write))
            {
                var root = new XmlRootAttribute("SearchKeywords");
                var ser = new XmlSerializer(typeof(SearchKeywords), root);
                if(sk.Keywords==null)
                {
                    sk = DefaultSearchKeywords();
                }
                ser.Serialize(fs, sk);
            }
        }

        /// <summary>
        /// Load search keywords from file, can change store file by call ChangeStoreFile
        /// </summary>
        /// <returns>SearchKeywords</returns>
        public SearchKeywords Load()
        {
            if( _searchKeywords == null )
            if (File.Exists(_keywordsfile))
            {
                using (var fs = new FileStream(_keywordsfile, FileMode.Open))
                {
                    var root = new XmlRootAttribute("SearchKeywords");
                    var ser = new XmlSerializer(typeof (SearchKeywords), root);
                    _searchKeywords = ser.Deserialize(fs) as SearchKeywords;
                }
            }
            if (_searchKeywords == null)
            {
                _searchKeywords = DefaultSearchKeywords();
                Save(_searchKeywords);
            }
            return _searchKeywords;
        }

        public bool IsStatusQualified(SinaStatus sinaStatus)
        {
            return IsStatusQualified(sinaStatus, _searchKeywords);
        }
        public bool IsStatusQualified(SinaStatus sinaStatus, SearchKeywords searchKeywords)
        {
            if(sinaStatus==null || sinaStatus.text==null || sinaStatus.user==null)
            {
                return false;
            }
            // location is a MUST match
            if (!string.IsNullOrEmpty(searchKeywords.City))
            {
                if (string.IsNullOrEmpty(searchKeywords.City) || !searchKeywords.City.Contains(sinaStatus.user.location))
                {
                    return false;
                }
            }
            bool result = searchKeywords.Keywords.Any(keyword => sinaStatus.text.Contains(keyword) || sinaStatus.user.description.Contains(keyword) || sinaStatus.user.screen_name.Contains(keyword));
            return result;
        }
    }
}
